<template>
  <div>

    <!-- 渲染角色列表 -->
    <el-checkbox-group v-model="roleIds">
      <el-checkbox v-for="item in rolesList" :key="item.id" :label="item.id">{{ item.name }}</el-checkbox>
    </el-checkbox-group>

    <el-row class="footer" type="flex" justify="center">
      <el-col :span="6">
        <el-button type="primary" size="small" @click="addRole">确定</el-button>
        <el-button size="small" @click="cancleDialog">取消</el-button>
      </el-col>
    </el-row>
  </div>
</template>

<script>
import { getRolesList } from '@/api/setting'
import { getUserDetailById } from '@/api/user'
import { assignRoles } from '@/api/employess'
export default {
  name: 'AssignRole',
  props: {
    rolesId: {
      type: [String, Number],
      required: true
    }
  },
  data() {
    return {
      roleIds: [], // 保存当前选中的权限列表
      rolesList: [] // 角色列表数据
    }
  },
  created() {
    // 调用 获取角色列表 的方法
    this.getRoles()
    // 调用 获取员工详情数据的方法
    this.getUserRoles()
  },
  methods: {
    // 员工角色分配
    async addRole() {
      const res = await assignRoles({ id: this.rolesId, roleIds: this.roleIds })
      console.log(res)
      this.$message.success(res.message)
      this.$emit('close')
      this.getUserRoles()
    },

    // 调用员工详情数据
    async getUserRoles() {
      const res = await getUserDetailById(this.rolesId)
      this.roleIds = res.data.roleIds
    },

    // 获取角色列表
    async  getRoles() {
      const res = await getRolesList()
      this.rolesList = res.data.rows
    },

    // 取消按钮
    cancleDialog() {
      this.$emit('close')
    }
  }
}
</script>

<style lang="scss" scoped>
.footer{
  margin-top: 35px;
}
</style>>

